package cn.com.dhcc.ddi.datasource.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.ddi.datamapping.service.DataMappingService;
import cn.com.dhcc.ddi.datasource.dao.TableInfoDao;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
/**
 * 
 * 表信息服务类
 * @Author: fxx
 * @date: 2017-5-16
 *
 */
@Service
public class TableInfoService extends BaseService {

	@Autowired
	TableInfoDao tableInfoDao;
	@Autowired
	DataMappingService dataMappingService;
	
	/**
	 * 
	 * @Title: getTableInfoByDsId
	 * @Description: 根据数据源id获取数据源表信息
	 * @param @param id
	 * @param @return
	 * @param @throws ServiceException 参数
	 * @return List<TableInfoVo> 返回类型
	 * @throws
	 */
	public List<TableInfoVo> getTableInfoByDsId(String id) throws ServiceException {
		try {
			return tableInfoDao.getTableInfoByDsId(id);
		} catch (DaoException e) {
			throw new ServiceException("根据数据源id获取数据源表信息失败", e);
		}
	}

	/**
	 * 
	 * @Title: getTableIdByDsId
	 * @Description: 根据数据源id获取表id集合
	 * @param @param dsId
	 * @param @return
	 * @param @throws ServiceException 参数
	 * @return List<String> 返回类型
	 * @throws
	 */
	public List<String> getTableIdByDsId(String dsId) throws ServiceException{
		try {
			return tableInfoDao.getTableIdByDsId(dsId);
		} catch (DaoException e) {
			throw new ServiceException("根据数据源id获取表id集合失败", e);
		} 
	}
	/**
	 * 
	 * @Title: deleteTbInfoById
	 * @Description: 根据数据源id删除表信息
	 * @param @param id
	 * @param @throws DaoException 参数
	 * @return void 返回类型
	 * @throws
	 */
    public void deleteTbInfoByDsId(String dsId) throws ServiceException {
    	try {
    		tableInfoDao.deleteTbInfoByDsId(dsId);
		} catch (DaoException e) {
			throw new ServiceException("根据数据源id删除表信息失败", e);
		}
    }    
    
	/**
	 * 
	 * @Title: updateTableInfo
	 * @Description: 更新或插入数据源表信息
	 * @param @param tbInfoList
	 * @param @throws ServiceException 参数
	 * @return void 返回类型
	 * @throws
	 */
    public void updateTableInfo(List<TableInfoVo> tbInfoList) throws ServiceException {
    	try {
    		//获取表名
    		List<String> tbNames = new ArrayList<String>();
    		String dsId = "";
    		int dataMappingCount = 0;
    		if (tbInfoList != null && tbInfoList.size() > 0) {
    			dsId = tbInfoList.get(0).getDsId();
    			dataMappingCount = dataMappingService.getCountByDsId(dsId);//查看是否与映射关联
    			if (dataMappingCount == 0) {
    				tbNames = tableInfoDao.getTbNamesByDsId(dsId);
    			}
    		}
    		//更新或插入
	    	for (int i = 0; i < tbInfoList.size(); i++) {
	    		TableInfoVo vo = tbInfoList.get(i);
	    		if(vo != null){
		    		int count = tableInfoDao.getCount(vo.getDsId(), vo.getSchema(), vo.getTableName());
		    		if (count == 1) {
		    			tableInfoDao.updateTableInfo(vo);
		    		} else {
		    			tableInfoDao.insertTableInfo(vo);
		    		}
		    		//移除存在的表名
		    		if(dataMappingCount == 0){
			    		tbNames.remove(vo.getTableName());
		    		}
	    		}
	    	}
	    	if (dataMappingCount == 0) {
	    		//删除数据库中不存在的表名
		    	for (int i = 0; i < tbNames.size(); i++) {
		    		tableInfoDao.deleteTbInfo(dsId, tbNames.get(i));
		    	}
	    	}
    	} catch (DaoException e) {
			throw new ServiceException("更新或插入数据源表信息失败", e);
		}
    }
    
    /**
     * 
     * @MethodName: [getById]
     * @Description:[通过id获取表信息]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-16]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [TableInfoVo]
     * @Throws:     [Exception]
     *
     */
    public TableInfoVo getById(String id) throws ServiceException {
    	try {
    		return tableInfoDao.getById(id);
		} catch (DaoException e) {
			throw new ServiceException("获取表信息失败", e);
		}
    }
    
    /**
     * 
     * @MethodName: [getTabInfoMap]
     * @Description:[获取表信息Map]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-16]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [Map<String,TableInfoVo>]
     * @Throws:     [Exception]
     *
     */
    public Map<String, TableInfoVo> getTabInfoMap(List<TableInfoVo> tableInfoList){
    	Map<String, TableInfoVo> map = new LinkedHashMap<String, TableInfoVo>();
    	for (TableInfoVo tabInfo:tableInfoList) {
    		Map<String, TableColVo> colMap =  new LinkedHashMap<String, TableColVo>();
    		List<TableColVo> cols = tabInfo.getTableColVos();
    		for (TableColVo col:cols) {
    			colMap.put(col.getColName(), col);
    		}
    		tabInfo.setTableColVos(null);
    		tabInfo.setTabColMap(colMap);
    		map.put(tabInfo.getTableName(), tabInfo);
    	}
    	return map;
    }
    
}
